# Create your views here.

from django.views import View

from server.utils import Response, Request, LoginRequiredPerson, Verify, Code


class LocationView(LoginRequiredPerson, View):
    """ User operation location information """

    def get(self, request):
        """ User get last location (latitude and longitude) information """
        user = request.user
        location = {'longitude': user.longitude, 'latitude': user.latitude}
        return Response.Json(location=location)

    @Request.getJson
    def post(self, request):
        """ User uploads current location (latitude and longitude) information """
        longitude = request.JSON.get('longitude')
        latitude = request.JSON.get('latitude')

        for code in [Verify.longitude(longitude), Verify.latitude(latitude)]:
            if code != Code.OK:
                return Response.Json(code=code)

        request.user.update_location(longitude, latitude)
        return Response.Json()
